<template>
  <div class="viewtasks">
    <el-page-header @back="goBack" content="任务详情" title="返回">
    </el-page-header>
    <el-card class="box-card mt-10">
      <div slot="header" class="clearfix" style="border-left: 2px solid #03a9ff;padding-left: 10px">
        <span>任务类型：{{data.type}}</span>
        <el-button type="text" style="float: right" @click="Do_tasks"
                   v-if="!compareTime(data.stopDate)&&this.taskStatus">做任务
        </el-button>
      </div>

      <el-row :gutter="20">
        <el-col :span="24">
          <p>提交内容： {{data.content}}</p>
          <p>要求： {{data.ask}}</p>
          <p>任务开始时间： {{data.releaseDate}}</p>
          <p>任务结束时间： {{data.stopDate}}</p>
        </el-col>
      </el-row>

    </el-card>
    <el-card class="mt-10">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column align="center" type="index" label="序号" width="180"></el-table-column>
            <el-table-column align="center" prop="teacherReleaseTask.type" label="任务类型"></el-table-column>
            <el-table-column align="center" prop="teacherReleaseTask.content" label="提交内容"></el-table-column>
            <el-table-column align="center" label="状态">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.studentsDoTask.state=='0'" type="info">未审核</el-tag>
                <el-tag v-if="scope.row.studentsDoTask.state=='2'" type="danger">已退回</el-tag>
                <el-tag v-if="scope.row.studentsDoTask.state=='1'" type="success">已通过</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button size="mini" @click="handleEdit(scope.$index,scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-card>

  </div>
</template>

<script>
    export default {
        name: "Viewtasks",
        data() {
            return {
                userInfo: this.UserInfo.getUserInfo(),
                tableData: [],
                data: [],
                allData: [],
                taskStatus: false,
            }
        },
        methods: {
            goBack() {
                this.$router.go(-1)
                // this.$router.push({
                //     path: '/student/index/information',
                //     query: {
                //         id: this.$route.query.all,
                //     }
                // })
            },
            handleEdit(index, row) {
                console.log(index, row)
                this.$router.push({
                    path: '/student/index/LookStuTask',
                    query: {
                        id: encodeURIComponent(JSON.stringify(this.data)),
                        row: encodeURIComponent(JSON.stringify(this.tableData[index])),
                    }
                })
            },
            Do_tasks() {
                this.$router.push({
                    path: '/student/index/StuDoTask',
                    query: {
                        id: encodeURIComponent(JSON.stringify(this.data)),
                    }
                })
            },
            selStuTask() {
                var self = this;
                console.log(this.allData)
                console.log(this.userInfo.id)
                var body = {
                    stuId: this.userInfo.id,
                    taskId: this.allData.teacherReleaseTask.id,
                }
                self.$http.post(self.URL.api + "studentsDoTasks/selStuTask", self.qs.stringify(body))
                    .then(function (response) {
                        console.log(response.data.data)
                        if (response.data.flag) {
                            self.tableData = response.data.data;
                        } else {

                        }
                    })
                    .catch(function (error) {

                    });
            },
            selStatus(id) {
                var self = this;
                var body = {
                    stuId: this.userInfo.id,
                    taskId: id,
                }
                self.$http.post(self.URL.api + "studentsDoTasks/selStuTaskStatus", self.qs.stringify(body))
                    .then(function (response) {
                        console.log(response.data.data)
                        if (response.data.data == null)
                            self.taskStatus = true
                        else if (response.data.flag)
                            if (response.data.data.status == "2")
                                self.taskStatus = true
                            else self.taskStatus = false;
                    }).catch(function (error) {
                });
            },
            compareTime(str){
                console.log(str);
                var d1=new Date(str).valueOf();
                var d2=new Date().valueOf();
                console.log(d2>d1);
                return d2>d1;
            },
        },
        mounted() {
            var dataAll = JSON.parse(decodeURIComponent(this.$route.query.id));
            console.log(dataAll)
            if (dataAll.teacherReleaseTask == undefined) {
                this.goBack();
            } else {
                this.data = dataAll.teacherReleaseTask;
                this.allData = dataAll;
                this.selStuTask();
                this.selStatus(dataAll.teacherReleaseTask.id);
            }
        },
    }
</script>

<style scoped>

</style>
